/*
 * CS 567 GUI - Semester Project - "DecisionLists"
 * Jason Frank
 * Spring 2011
 *
 * This File:  Container for the main center view of the app.  It replaces the center panel (upon
 *             the user changing views) with the desired new view.  I am implementing this container
 *             as a regular view so that it is notified of the desired view change.  Hopefully I'll
 *             implement fading from the old view to the new view to enhance the visual cue of the
 *             changed view.
 *
 *             The main app class only instantiates this class, rather than all possible views that
 *             can occupy the center area. Therefore, this class instantiates all of the center-area views.
 */

package Decision_Lists;

//import java.awt.AlphaComposite;
//import java.awt.Color;
import java.awt.Component;
//import java.awt.Composite;
import java.awt.Dimension;
//import java.awt.FlowLayout;
import java.awt.Graphics;
//import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
//import javax.swing.JLayeredPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
//import javax.swing.border.Border;

/*
 */
public class CenterPanel2 extends JPanel implements DL_ViewInterface /*, ActionListener */ {

  boolean test_fade = false;
  boolean test_change_views = false;
  final String view_name = "CenterPanel";
  boolean is_center_view = false;
  DL_Model model;
  int panelWidthPref  = 850;
  int panelHeightPref = 600;
  DL_ViewInterface current_center_view;
  DL_ViewInterface butterfly;
  DL_ViewInterface spreadsheet;

  DL_ViewInterface initialView; // must be set after the actual view has been instantiated
  // This map holds a ref to all views. The class can use this map to find a ref to the new desired view
  //  when the user selects a different view from the drop-down menu.
  // Making this map static b/c the combobox in HeaderView needs to know which view
  //  corresponds to the string title of the view, so that it can call model.setCurView().
                // Name,   View
  static TreeMap<String, DL_ViewInterface> views_map = new TreeMap<String, DL_ViewInterface>();
  ////////////////////////////////////////////
  ///// FADE MEMBERS /////////////////////////
  private final int fade_min_opacity = 0;
  private final int fade_max_opacity = 255;


  //private JLayeredPane layeredPane;
  //Integer layered_level = 0;  // increment as I go

  JPanel newView;  // used in fade timers
  JPanel oldView;

  //Panel_Fader2 pf;
  Panel_Fader3 pf3;

  /////////// END OBJECT VARS ///////////////////
  ///////////////////////////////////////////////
  
  ///////////////////
  // Constructor
  public CenterPanel2(DL_Model model) throws Exception  {
    
    this.model = model;           // link the model to this view
    this.model.addObserver(this, this.view_name, is_center_view); // link this view to the model

    // INITIAL LAYOUT
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    setBorder(BorderFactory.createEmptyBorder(0,15,20,15));  // top,left,bottom,right

    // INSTANTIATE ALL CENTER VIEWS and add them to map
    butterfly = new ButterflyView(model);
    views_map.put(butterfly.getViewName(), butterfly);
    spreadsheet = new SpreadsheetView(model);
    views_map.put(spreadsheet.getViewName(), spreadsheet);


    // Set the CURRENT VIEW in this center panel
    current_center_view = butterfly;
//    add((Component)current_center_view);


    
    // Must also set the current view in the model
    initialView = butterfly;
    model.setInitialView(initialView);

    // Trying to use the Panel_Fader obj as a strategy obj to do the fading
        //pf = new Panel_Fader2(this, this.getGraphics(), oldView, newView);

    pf3 = new Panel_Fader3(this, (JPanel)current_center_view);
    
  } ///// end constructor /////


  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    pf3.paint_caller_panel(g);

  } //// end paintComponent() ///////

  /////////////////////////////////////////////////
  ////// Methods Required by the Interface ////////

  // The Model calls this to notify this View of a changed current view
  // This method removes the previous center view and adds the new center view.
  public void curViewChanged(String old_view_name, String new_view_name ) {

    /////////////////////////
    //// START FADES   //////

    newView = (JPanel) views_map.get(new_view_name);
    oldView = (JPanel) current_center_view;

    pf3.setFadein_panel(newView);
    // Start the fade
    try {
      pf3.startFade();
    } catch (Exception ex) {
      Logger.getLogger(CenterPanel2.class.getName()).log(Level.SEVERE, null, ex);
    }

  } ///// end curViewChanged() //////////

  // Model calls this to inform views of an added (new) list item.
  //  The views will then probably need to re-layout their list items to reflect the change.
  public void itemAdded(List_Item_Data newItem) {
    // Do nothing
  }
  // Model calls this to inform views of a changed list item (i.e., new item added, old item deleted, etc).
  //  The views will then probably need to re-layout their list items to reflect the change.
  public void itemChanged() {
    // Do nothing, this view doesn't care about new list items
  }

  public String getViewName() {
    return this.view_name;
  } //// end getViewName() /////////

  /////// end required by interface functions  //////
  ///////////////////////////////////////////////////


  ////// SETTERS ////
  public void setCurrent_center_view(DL_ViewInterface view) {
    this.current_center_view = view;
  }



  @Override
  public Dimension getMinimumSize()   { return getPreferredSize(); }
  @Override  // TODO: set the width as the width of the parent
  public Dimension getPreferredSize() { return new Dimension(panelWidthPref, panelHeightPref); }

} //////////////////// end class CenterPanel //////////////////

